﻿using APP.QUANLY.Common;
using MetroUI.Forms;
using RMAS;
using RMAS.DAL;
using Sys;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace RMAS
{
    public partial class frmDM_Menu : Form
    {
        EnumGUI.ThaoTac thaotac;
        DM_MENU_Provider prov = null;
        DM_MENU Currobj = null;
        private SqlConnection con;

        public frmDM_Menu(DM_MENU _obj, EnumGUI.ThaoTac _thaotac)
        {
            InitializeComponent();
            this.thaotac = _thaotac;
            this.Currobj = _obj;
        }
        private void frmDM_Menu_Load(object sender, EventArgs e)
        {
            LoadAll();
        }
        private void LoadAll()
        {
            con = (SqlConnection)StaticVar.ServerConnection;
            prov = new DM_MENU_Provider();
            LoadData();
            ControlUI(thaotac);
        }
        public void LoadData()
        {
            LoadToCmb_GroupMenu();
            LoadToCmb_Unit1();
            LoadToCmb_Unit2();
        }

        private void LoadToCmb_GroupMenu()
        {
            try
            {
                DM_GROUPMENUList list = StaticVar.DanhSachNhomThucDon;
                cmbGroupMenu.DataSource = null;

                if (list.Count==0)
                {
                    if (MessageBox.Show("Chưa có danh sách nhóm thực đơn!\nVui lòng nhập danh sách nhóm thực đơn trước!!!", "Thông Báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                    {
                        frmDM_GroupMenu Grmenu = new frmDM_GroupMenu(null, EnumGUI.ThaoTac.them);
                        if (Grmenu.ShowDialog() == DialogResult.OK) list = StaticVar.DanhSachNhomThucDon;
                    }
                    else
                    {
                        this.DialogResult = DialogResult.Cancel;
                        base.Close();
                    }
                }
                DataTable data = new DataTable();
                data.Columns.Add("Mã", typeof(string));
                data.Columns.Add("nhóm thực đơn", typeof(string));
                foreach (DM_GROUPMENU obj in list)
                {
                    data.Rows.Add(obj.ID, obj.Name);
                }

                cmbGroupMenu.DataSource = data;
                cmbGroupMenu.DisplayMember = "nhóm thực đơn";
                cmbGroupMenu.ValueMember = "Mã";
                cmbGroupMenu.ColumnWidths = "0,200";
                cmbGroupMenu.SelectedIndex = -1;
                cmbGroupMenu.Text = "";
            }
            catch { }
        }
        private void LoadToCmb_Unit1()
        {
            try
            {
                DM_UNITList list = StaticVar.DanhSachDonVi;
                cmbUnit1.DataSource = null;

                if (list.Count == 0)
                {
                    if (MessageBox.Show("Chưa có danh sách đơn vị!\nVui lòng nhập danh sách đơn vị trước!!!", "Thông Báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                    {
                        frmDM_Unit fuint = new frmDM_Unit(null, EnumGUI.ThaoTac.them);
                        if (fuint.ShowDialog() == DialogResult.OK) list = StaticVar.DanhSachDonVi;
                    }
                    else
                    {
                        this.DialogResult = DialogResult.Cancel;
                        base.Close();
                    }
                }
                DataTable data = new DataTable();
                data.Columns.Add("Mã", typeof(string));
                data.Columns.Add("Đơn vị", typeof(string));
                foreach (DM_UNIT obj in list)
                {
                    data.Rows.Add(obj.ID, obj.Name);
                }

                cmbUnit1.DataSource = data;
                cmbUnit1.DisplayMember = "Đơn vị";
                cmbUnit1.ValueMember = "Mã";
                cmbUnit1.ColumnWidths = "0,200";
                cmbUnit1.SelectedIndex = -1;
                cmbUnit1.Text = "";
            }
            catch { }
        }
        private void LoadToCmb_Unit2()
        {
            try
            {
                DM_UNITList list = StaticVar.DanhSachDonVi;
                cmbUnit2.DataSource = null;

                if (list.Count==0)
                {
                    if (MessageBox.Show("Chưa có danh sách đơn vị!\nVui lòng nhập danh sách đơn vị trước!!!", "Thông Báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                    {
                        frmDM_Unit fuint = new frmDM_Unit(null, EnumGUI.ThaoTac.them);
                        if (fuint.ShowDialog() == DialogResult.OK) list = StaticVar.DanhSachDonVi;
                    }
                    else
                    {
                        this.DialogResult = DialogResult.Cancel;
                        base.Close();
                    }
                }
                DataTable data = new DataTable();
                data.Columns.Add("Mã", typeof(string));
                data.Columns.Add("Đơn vị", typeof(string));
                foreach (DM_UNIT obj in list)
                {
                    data.Rows.Add(obj.ID, obj.Name);
                }

                cmbUnit2.DataSource = data;
                cmbUnit2.DisplayMember = "Đơn vị";
                cmbUnit2.ValueMember = "Mã";
                cmbUnit2.ColumnWidths = "0,200";
                cmbUnit2.SelectedIndex = -1;
                cmbUnit2.Text = "";
            }
            catch { }
        }
        private void ControlUI(EnumGUI.ThaoTac thaotac)
        {
            if (thaotac == EnumGUI.ThaoTac.them || thaotac == EnumGUI.ThaoTac.sua)
            {
                SetTextReadOnly(false);
            }
            if (thaotac == EnumGUI.ThaoTac.xem)
            {
                SetTextReadOnly(true);
            }
            SetStateButton(thaotac);
            loadToUI(Currobj);
            UIHelper.LoadFocus(txtName);
        }
        private void SetStateButton(EnumGUI.ThaoTac thaotac)
        {
            switch (thaotac)
            { 
                case EnumGUI.ThaoTac.them:
                      btnLuuThem.Visible = true;
                      btnLuuThoat.Visible = true;
                      btnMacDinh.Visible = true;
                    break;
                case EnumGUI.ThaoTac.sua:
                      btnLuuThem.Visible = false;
                      btnLuuThoat.Visible = true;
                      btnMacDinh.Visible = true;
                    break;
                case EnumGUI.ThaoTac.xem:
                    btnLuuThem.Visible = false;
                    btnLuuThoat.Visible = false;
                    btnMacDinh.Visible = false;
                    break;
            }
          
        }
        private void SetTextReadOnly(bool IsReadOnly)
        {
            txtID.ReadOnly = IsReadOnly;
            txtBarcode.ReadOnly = IsReadOnly;
            txtName.ReadOnly = IsReadOnly;
            txtNote.ReadOnly = IsReadOnly;

            cmbGroupMenu.Enabled = !IsReadOnly;
            cmbUnit1.Enabled = !IsReadOnly;
            cmbUnit2.Enabled = !IsReadOnly;

            txtNote.Enabled = !IsReadOnly;
            txtXuatXu.Enabled = !IsReadOnly;

            numQuyCach.Enabled = !IsReadOnly;
            numGiaLe.Enabled = !IsReadOnly;
            numGiaNhap.Enabled = !IsReadOnly;
            numGiaSauVAT.Enabled = !IsReadOnly;
            numGiaSi.Enabled = !IsReadOnly;
            numGiaSauVAT.Enabled = !IsReadOnly;

            ptbHinh.Enabled = !IsReadOnly; 
            btnChonHinh.Enabled = !IsReadOnly;
            btnXoaHinh.Enabled = !IsReadOnly;

            chbIsUse.Enabled = !IsReadOnly;

        }

        private bool loadToUI(DM_MENU obj)
        {
            bool bKQ = false;
            try
            {
                if (obj == null)
                {
                    obj = new DM_MENU();
                    DM_MENUList list = prov.GetAll();
                    if (list == null) obj.ID = "M.1";
                    else obj.ID = "M." + (list.Count + 1).ToString();
                    obj.PrID = obj.ID;
                    obj.IDPOS = StaticVar.CPUID;
                    obj.Number = 1;
                    obj.IsUse = true;
                    obj.DateCreate = DateTime.Now;
                    obj.UserNameCreate = StaticVar.CurrUSER.UserName;
                    
                }

                txtID.Text = obj.ID;
                txtBarcode.Text = obj.PrID;
                txtName.Text = obj.PrName;
                txtXuatXu.Text = obj.PrMadeIn;
                numQuyCach.Value = (Decimal)obj.Number;
                numGiaNhap.Value = (Decimal)obj.PrPrice_Purchase;
                numGiaSauVAT.Value = (Decimal)obj.PrPrice_AfterVAT;
                numGiaSi.Value = (Decimal)obj.PrPrice_WholeSale;
                numGiaLe.Value = (Decimal)obj.PrPrice_RetailSale;
                numVAT.Value = (Decimal)obj.PrVAT;
                chbIsUse.Checked = obj.IsUse;
                txtNote.Text = obj.Note;

                labPosID.Text = obj.IDPOS;
                labGrmenuID.Text = obj.GroupMenuID;
                labUint1ID.Text = obj.UnitID1;
                labUnit2ID.Text = obj.UnitID2;

                if (StaticVar.DanhSachNhomThucDon != null && StaticVar.DanhSachNhomThucDon.Count > 0)
                {
                    DM_GROUPMENU objGr = StaticVar.DanhSachNhomThucDon.Find(obj.GroupMenuID);
                    if (objGr != null) cmbGroupMenu.Text = objGr.Name;
                }

                if (StaticVar.DanhSachDonVi != null && StaticVar.DanhSachDonVi.Count > 0)
                {
                    DM_UNIT objUn1 = StaticVar.DanhSachDonVi.Find(obj.UnitID1);
                    if (objUn1 != null) cmbUnit1.Text = objUn1.Name;

                    DM_UNIT objUn2 = StaticVar.DanhSachDonVi.Find(obj.UnitID2);
                    if (objUn2 != null) cmbUnit2.Text = objUn2.Name;
                }

                ptbHinh.Image = ImageUtil.GetImageFromBytes(obj.PrImage);

                if (numGiaNhap.Value == 0) numGiaSauVAT.ResetText();
                if (numGiaSauVAT.Value == 0) numGiaSauVAT.ResetText();
                if (numGiaSi.Value == 0) numGiaSauVAT.ResetText();
                if (numGiaLe.Value == 0) numGiaSauVAT.ResetText();
                if (numQuyCach.Value == 0) numGiaSauVAT.ResetText();
                bKQ = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show("Lỗi :" + ex.Message, "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            return bKQ;
        }
        private DM_MENU loadToValues()
        {
            DM_MENU obj = new DM_MENU();
            try
            {
                if (labPosID.Text != "" && txtName.Text != "" && labGrmenuID.Text != "" && labUint1ID.Text!="" && labUnit2ID.Text!="")
                {
                    byte[] hinh = ImageUtil.GetBytesFromImage(ptbHinh.Image);
                    obj.Fill(txtBarcode.Text,txtID.Text,txtName.Text,hinh,labGrmenuID.Text,labUint1ID.Text,labUnit2ID.Text,(double)numQuyCach.Value,(double)numVAT.Value,txtXuatXu.Text,numGiaNhap.Value,numGiaSauVAT.Value,numGiaLe.Value,numGiaLe.Value,txtNote.Text,StaticVar.CurrUSER.UserName,DateTime.Parse(labCreateDate.Text),labPosID.Text,chbIsUse.Checked,false);
                }
                else
                {
                    MessageBox.Show("Vui lòng nhập đầy đủ thông tin!", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    obj = null;
                }
            }
            catch
            {
                obj = null;
            }
            return obj;
        }
        private bool cmd_XuLy(EnumGUI.ThaoTac thaotac)
        {
            bool KQ = false;
            if (thaotac == EnumGUI.ThaoTac.macdinh) KQ = loadToUI(Currobj);
            else
            {
                DM_MENU obj = loadToValues();
                if (obj != null)
                {
                    switch (thaotac)
                    {
                        case EnumGUI.ThaoTac.them:
                            KQ = prov.Cmd_ThemDM_MENU(obj);
                            break;
                        case EnumGUI.ThaoTac.sua:
                            KQ = prov.Cmd_CapNhatDM_MENU(obj);
                            break;
                    }
                    if (prov.ErrMsg != null)
                        MessageBox.Show(prov.ErrMsg.Replace("DM_MENU", "thực đơn"), "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            return KQ;
        }

        private void btn_Click(object sender, EventArgs e)
        {
            bool KQ = false;
            Button btn = (Button)sender;
            string _thaotac = btn.Tag.ToString();
            switch (_thaotac)
            { 
                case "luuthoat":
                    KQ = cmd_XuLy(thaotac);
                    if (KQ)
                    {
                        this.DialogResult = DialogResult.OK;
                        base.Close();
                    }
                    break;
                case "luuthem":
                    KQ = cmd_XuLy(thaotac);
                    if (KQ)
                    {
                        ControlUI(EnumGUI.ThaoTac.them);
                    }
                    break;
                case "macdinh":
                    KQ = cmd_XuLy(EnumGUI.ThaoTac.macdinh);
                    break;
                case "themnhom":
                     frmDM_GroupMenu Grmenu = new frmDM_GroupMenu(null, EnumGUI.ThaoTac.them);
                     if (Grmenu.ShowDialog() == DialogResult.OK) 
                     {
                         LoadToCmb_GroupMenu();
                     }
                    break;
                case "themdonvi":
                    frmDM_Unit fuint = new frmDM_Unit(null, EnumGUI.ThaoTac.them);
                    if (fuint.ShowDialog() == DialogResult.OK)
                    {
                        LoadToCmb_Unit1();
                        LoadToCmb_Unit2();
                    }
                    break;
                case "thoat":
                    this.DialogResult = DialogResult.OK;
                    base.Close();
                    break;
            }
            StaticVar.DanhSachThucDon = prov.GetAll();
            UIHelper.LoadFocus(txtName);
        }
        private void txt_Enter(object sender, EventArgs e)
        {
            UIHelper.txt_Enter(sender, e);
        }
        private void txt_Leave(object sender, EventArgs e)
        {
            UIHelper.txt_Leave(sender, e);
        }
        private void txt_KeyPress(object sender, KeyPressEventArgs e)
        {
            try
            {
                TextBox txt = sender as TextBox;
                if (txt.Name == txt.Name)
                    UIHelper.txt_KeyPress(sender, txtBarcode, e);
                if (txt.Name == txtBarcode.Name)
                    UIHelper.txt_KeyPress(sender, txtName, e);
                if (txt.Name == txtName.Name)
                    UIHelper.txt_KeyPress(sender, cmbGroupMenu, e);
                if (txt.Name == cmbGroupMenu.Name)
                    UIHelper.txt_KeyPress(sender, txtXuatXu, e);
                if (txt.Name == txtXuatXu.Name)
                    UIHelper.txt_KeyPress(sender, cmbUnit1, e);
                if (txt.Name == cmbUnit1.Name)
                    UIHelper.txt_KeyPress(sender, cmbUnit2, e);
                if (txt.Name == cmbUnit2.Name)
                    UIHelper.txt_KeyPress(sender, numQuyCach, e);
                if (txt.Name == numQuyCach.Name)
                    UIHelper.txt_KeyPress(sender, numGiaNhap, e);
                if (txt.Name == numGiaNhap.Name)
                    UIHelper.txt_KeyPress(sender, numGiaSauVAT, e);
                if (txt.Name == numGiaSauVAT.Name)
                    UIHelper.txt_KeyPress(sender, numGiaLe, e);
                if (txt.Name == numGiaLe.Name)
                    UIHelper.txt_KeyPress(sender, numGiaSi, e);
                if (txt.Name == numGiaSi.Name)
                    UIHelper.txt_KeyPress(sender, numVAT, e);
                if (txt.Name == numVAT.Name)
                    UIHelper.txt_KeyPress(sender, txtNote, e);
                if (txt.Name == txtNote.Name)
                    UIHelper.txt_KeyPress(sender, btnLuuThoat, e);
            }
            catch { }
        }

        private void cmbGroupMenu_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbGroupMenu.SelectedIndex >= 0)
            {
                DataRowView drvData = (DataRowView)cmbGroupMenu.SelectedItem;
                string Ma = drvData.Row.ItemArray[0].ToString();
                string Ten = drvData.Row.ItemArray[1].ToString();
                labGrmenuID.Text = Ma;
                cmbGroupMenu.Text = Ten;
            }
        }
        private void cmbGroupMenu_OpenSearchForm(object sender, EventArgs e)
        {
            frmSearchInfo frmSearch = new frmSearchInfo((MultiColumnComboBox)sender);
            frmSearch.ShowDialog();
            if (cmbGroupMenu.SelectedIndex >= 0)
            {
                DataRowView drvData = (DataRowView)cmbGroupMenu.SelectedItem;
                string Ma = drvData.Row.ItemArray[0].ToString();
                string Ten = drvData.Row.ItemArray[1].ToString();
                labGrmenuID.Text = Ma;
                cmbGroupMenu.Text = Ten;
            }
        }

        private void cmbUnit1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbUnit1.SelectedIndex >= 0)
            {
                DataRowView drvData = (DataRowView)cmbUnit1.SelectedItem;
                string Ma = drvData.Row.ItemArray[0].ToString();
                string Ten = drvData.Row.ItemArray[1].ToString();
                labUint1ID.Text = Ma;
                cmbUnit1.Text = Ten;
            }
        }
        private void cmbUnit1_OpenSearchForm(object sender, EventArgs e)
        {
            frmSearchInfo frmSearch = new frmSearchInfo((MultiColumnComboBox)sender);
            frmSearch.ShowDialog();
            if (cmbUnit1.SelectedIndex >= 0)
            {
                DataRowView drvData = (DataRowView)cmbUnit1.SelectedItem;
                string Ma = drvData.Row.ItemArray[0].ToString();
                string Ten = drvData.Row.ItemArray[1].ToString();
                labUint1ID.Text = Ma;
                cmbUnit1.Text = Ten;
            }
        }

        private void cmbUnit2_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbUnit2.SelectedIndex >= 0)
            {
                DataRowView drvData = (DataRowView)cmbUnit2.SelectedItem;
                string Ma = drvData.Row.ItemArray[0].ToString();
                string Ten = drvData.Row.ItemArray[1].ToString();
                labUnit2ID.Text = Ma;
                cmbUnit2.Text = Ten;
            }
        }
        private void cmbUnit2_OpenSearchForm(object sender, EventArgs e)
        {
            frmSearchInfo frmSearch = new frmSearchInfo((MultiColumnComboBox)sender);
            frmSearch.ShowDialog();
            if (cmbUnit2.SelectedIndex >= 0)
            {
                DataRowView drvData = (DataRowView)cmbUnit2.SelectedItem;
                string Ma = drvData.Row.ItemArray[0].ToString();
                string Ten = drvData.Row.ItemArray[1].ToString();
                labUnit2ID.Text = Ma;
                cmbUnit2.Text = Ten;
            }
        }

        private void btnChonHinh_Click(object sender, EventArgs e)
        {
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.Filter = "Image Files (*.png *.jpg *.bmp) |*.png; *.jpg; *.bmp|All Files(*.*) |*.*";
            dialog.InitialDirectory = @"C:\";
            dialog.Title = "Vui lòng chọn một hình đại điện cho công ty.";
            dialog.Multiselect = false;
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                ptbHinh.Image = Image.FromFile(dialog.FileName);
            }
            dialog.Dispose();
        }
        private void btnXoaHinh_Click(object sender, EventArgs e)
        {
            ptbHinh.Image = null;
        }
    }
}
